/*************************
 * StringOrderedPair.java
 * @author odedmagg
 * An ordered pair of strings.
 * Last updater: Oded
 * Date: 29.7.09 15:00
 */

public class StringOrderedPair {
	private String _str[];
	
	public StringOrderedPair(String str1, String str2)
	{
		_str = new String[2];
		if (str1.compareTo(str2) < 0)
		{
			_str[0] = str1;
			_str[1] = str2;
		}
		else {
			_str[1] = str1;
			_str[0] = str2;			
		}
	}
	
	public StringOrderedPair(StringOrderedPair sop) {
		_str = sop._str.clone();
	}
	
	public String getString1() {
		return new String(_str[0]);
	}
	
	public String getString2() {
		return new String(_str[1]);
	}
	
	public String[] getStrings() {
		String[] strs = {_str[0], _str[1]};
		return strs;
	}
	
	/**
	 * Overwrites the equals of the Java object class. 
	 */
	public boolean equals(Object obj) {
		if (obj instanceof StringOrderedPair) {
			StringOrderedPair otherPair = (StringOrderedPair)obj;
			return (_str[0].equals(otherPair._str[0])) && 
			       (_str[1].equals(otherPair._str[1])); 
		}
		return false;
	}

	public int compareTo(Object o) throws ClassCastException {
		if (o instanceof StringOrderedPair) {
			return compareTo((StringOrderedPair)o); 
		}
		else {
			throw new ClassCastException();
		}
	}
	
	public int compareTo(StringOrderedPair other) {
		int firstComp = _str[0].compareTo(other.getString1());
		if (firstComp != 0)
			return firstComp;
		
		return _str[1].compareTo(other.getString2());
	}
	
	/**
	 * hashCode - use the hash code of both names.
	 */
	public int hashCode() {
		return (_str[0]+_str[1]).hashCode();
	}
}
